<?php
require_once('config.php');

include('IXR_Library.inc.php');
$dbConn = mysql_connect (DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db(DB_DATABASE) or die('Cannot select database. ' . mysql_error());
mysql_query("SET NAMES utf8");


function dbQuery($sql){	
dump($sql);
$result = mysql_query($sql) or die(mysql_error());
	return $result;
}


function dump($value='') 
{
$fp = fopen("xmlrpc.log", 'a');
fwrite($fp, $_SERVER['REMOTE_ADDR'] . " "  . " " . $value."\r\n");
fclose($fp);
} 

function mysql_resultTo2DAssocArray ( $result) {
   $i=0;
   $ret = array();
   while ($row = mysql_fetch_assoc($result)) {
       foreach ($row as $key => $value) {
           $ret[$i][$key] = $value;
           }
       $i++;
       }
   return ($ret);
}

function getSQL($args) {
dump($args); 
//return   mysql_resultTo2DAssocArray( dbQuery($args));
}


function getProduct() {
return   mysql_resultTo2DAssocArray(dbQuery(
    "SELECT p.model, d.name, p.quantity, p.price
    FROM ".DB_PREFIX."product p
    Join ".DB_PREFIX."product_description d ON (d.product_id=p.product_id) 
    "
));
}


function setPriceQuantity($args) {
	dbQuery("UPDATE ".DB_PREFIX."product SET quantity=" . $args[1] . ", price=" . $args[2] . " WHERE model='" . $args[0]. "'");
}




function add($args) {
return $args[0];
//    return $args[0] + $args[1];
}

function addArray($array) {
    $total = 0;
    foreach ($array as $number) {
        $total += $number;
    }
    return implode(' + ', $array).' = '.$total;
}  



$server = new IXR_Server(array(
'shopscript.getProduct' => 'getProduct',
'shopscript.setPriceQuantity' => 'setPriceQuantity'
));
?>